package Batch::Simple;
#use Modern::Perl;
use strict;
use warnings;
use Moose;
use Batch::Simple::Result;
use autodie qw{open close};
use File::Temp;

# JAVi
# javi# hueso

# arturo

# ola

our $VERSION = '0.01';

has 'command' => ( 
  is => 'rw',
  isa => 'Str',
  required => 1,
);

has 'options' => (
  is => 'rw',
  isa => 'ArrayRef[Str]',
  default => sub { [] },
);

has 'input' => ( 
  is => 'rw',
  isa => 'Str',
  clearer   => 'clear_input',
  predicate => 'has_input',
);

around BUILDARGS => sub {
      my $orig = shift;
      my $class = shift;

      if ( @_ == 1 && ! ref $_[0] ) {
          return $class->$orig(command => $_[0]);
      }
      else {
          return $class->$orig(@_);
      }
  };

sub run {
  my $self = shift;

  my $teh = File::Temp->new();
  my $ten = $teh->filename();


  my $savein;
  if ($self->has_input) {
    open $savein, '<&', \*STDIN;
    my $inputname = $self->input;
    die "Error in Batch::Simple::run. Can't find input file '$inputname'\n" unless -r $inputname;
    open STDIN, '<', $inputname;
  }
  open my $saveerr, ">& STDERR";

    open STDERR, ">&", $teh;

      my $perlWarnings = '';

        my ($command, @options) = ($self->command, @{$self->options});
        my $out = do {
          local $SIG{'__WARN__'} = sub { $perlWarnings .= $_[0] };
          qx{$command @options};
        };

        $out = '' unless defined($out);

        my $status = $?;

    close($teh); # and close STDERR

    open my $eh, "<", $ten;
      my $err = join '', <$eh>;
    close($eh);
    $err .= $perlWarnings;

  open STDERR, '>&', $saveerr; # restore STDERR
  open STDIN,  '<&', $savein if $self->has_input;

  my $r = Batch::Simple::Result->new(stdout => $out, stderr => $err, status => $status);
  return $r;
}

1;
__END__

=head1 NAME

Batch::Simple - OOP version extension of qx

=head1 SYNOPSIS

  use Batch::Simple;

  my $x = Batch::Simple->new(command => q{echo 'hola'});
  my $r = $x->run;
  my $expected = Batch::Simple::Result->new( 'status' => 0, 'stderr' => '', 'stdout' => "hola\n");
  is_deeply($r, $expected, q{execution of 'echo'});
  is("$r", "hola\n", 'toString overloading works');

  my $x = Batch::Simple->new(command => q{echo}, options => [qw{hola  juan}]);
  my $r = $x->run;
  my $expected = Batch::Simple::Result->new( 'status' => 0, 'stderr' => '', 'stdout' => "hola juan\n");
  is_deeply($r, $expected, q{execution of 'echo' with options});

  my $x = Batch::Simple->new(q{echo 'hola'});
  my $r = $x->run;
  my $expected = Batch::Simple::Result->new( 'status' => 0, 'stderr' => '', 'stdout' => "hola\n");
  is_deeply($r, $expected, q{Not named call. Execution of 'echo'});

  my $s = 0;
  if ($r) { $s = 1 };
  is($s, 1, 'Overloading to bool works');

=head1 AUTHOR

casiano, E<lt>casiano.rodriguez.leon@gmail.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2011 by casiano

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.10.0 or,
at your option, any later version of Perl 5 you may have available.


=cut
